package com.farmer.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farmer.common.result.Result;
import com.farmer.finance.mapper.FinanceServiceMapper;
import com.farmer.finance.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private FinanceServiceMapper financeServiceMapper;

    @Override
    public Result<?> list(String type, Long userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<com.farmer.common.entity.FinanceService> wrapper = new QueryWrapper<>();
        if (type != null && !type.isEmpty())
            wrapper.eq("type", type);
        if (userId != null)
            wrapper.eq("user_id", userId);
        Page<com.farmer.common.entity.FinanceService> page = new Page<>(pageNum, pageSize);
        Page<com.farmer.common.entity.FinanceService> result = financeServiceMapper.selectPage(page, wrapper);
        return Result.success("查询成功", result);
    }

    @Override
    public Result<?> add(com.farmer.common.entity.FinanceService entity) {
        int res = financeServiceMapper.insert(entity);
        return res > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @Override
    public Result<?> update(com.farmer.common.entity.FinanceService entity) {
        int res = financeServiceMapper.updateById(entity);
        return res > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @Override
    public Result<?> delete(Long id) {
        int res = financeServiceMapper.deleteById(id);
        return res > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Override
    public Result<?> detail(Long id) {
        com.farmer.common.entity.FinanceService entity = financeServiceMapper.selectById(id);
        return entity != null ? Result.success("查询成功", entity) : Result.error("未找到记录");
    }
}